package com.one.common.utils;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;


import com.one.common.CommonApp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class ResourceUtils {
	private static Resources sResources;

	public static Resources getResources() {
		if (sResources == null) {
			sResources = CommonApp.getInstance().getResources();
		}
		return sResources;
	}

	private ResourceUtils() {
		throw new AssertionError();
	}

	/**
	 * get an asset using ACCESS_STREAMING mode. This provides access to files
	 * that have been bundled with an application as assets -- that is, files
	 * placed in to the "assets" directory.
	 *
	 * @param fileName
	 *            The name of the asset to open. This name can be hierarchical.
	 * @return
	 */
	public static String geFileFromAssets(String fileName) {
		if (TextUtils.isEmpty(fileName)) {
			return null;
		}

		StringBuilder s = new StringBuilder("");
		try {
			InputStreamReader in = new InputStreamReader(getResources().getAssets().open(fileName));
			BufferedReader br = new BufferedReader(in);
			String line;
			while ((line = br.readLine()) != null) {
				s.append(line);
			}
			return s.toString();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * get content from a raw resource. This can only be used with resources
	 * whose value is the name of an asset files -- that is, it can be used to
	 * open drawable, sound, and raw resources; it will fail on string and color
	 * resources.
	 *
	 * @param resId
	 *            The resource identifier to open, as generated by the appt
	 *            tool.
	 * @return
	 */
	public static String geFileFromRaw(int resId) {
		StringBuilder s = new StringBuilder();
		try {
			InputStreamReader in = new InputStreamReader(getResources().openRawResource(resId));
			BufferedReader br = new BufferedReader(in);
			String line;
			while ((line = br.readLine()) != null) {
				s.append(line);
			}
			return s.toString();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * @param fileName
	 * @return
	 */
	public static List<String> geFileToListFromAssets(String fileName) {
		if (TextUtils.isEmpty(fileName)) {
			return null;
		}

		List<String> fileContent = new ArrayList<String>();
		try {
			InputStreamReader in = new InputStreamReader(getResources().getAssets().open(fileName));
			BufferedReader br = new BufferedReader(in);
			String line;
			while ((line = br.readLine()) != null) {
				fileContent.add(line);
			}
			br.close();
			return fileContent;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * @param resId
	 * @return
	 */
	public static List<String> geFileToListFromRaw(int resId) {
		List<String> fileContent = new ArrayList<String>();
		BufferedReader reader = null;
		try {
			InputStreamReader in = new InputStreamReader(getResources().openRawResource(resId));
			reader = new BufferedReader(in);
			String line = null;
			while ((line = reader.readLine()) != null) {
				fileContent.add(line);
			}
			reader.close();
			return fileContent;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 根据资源的名字获取其ID值
	 * 
	 * @author huangb
	 *
	 */
	public static int getIdByName(Context context, String className, String name) {
		String packageName = context.getPackageName();
		Class r = null;
		int id = 0;
		try {
			r = Class.forName(packageName + ".R");

			Class[] classes = r.getClasses();
			Class desireClass = null;

			for (int i = 0; i < classes.length; ++i) {
				if (classes[i].getName().split("\\$")[1].equals(className)) {
					desireClass = classes[i];
					break;
				}
			}

			if (desireClass != null)
				id = desireClass.getField(name).getInt(desireClass);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		}

		return id;
	}

	/**
	 * 获取styleable的ID号
	 * 
	 * @paramcontext
	 * @param name
	 * @return
	 */
	public static int getStyleable(Context context, String name) {
		return ((Integer) getIdByName(context, "styleable", name)).intValue();
	}

	/**
	 * 获取styleable的ID号数组
	 * 
	 * @paramcontext
	 * @param name
	 * @return
	 */

	public static int[] getStyleableArray(Context context, String name) {
		return (int[]) getIdArrByName(context, "styleable", name);
	}

	/**
	 * 根据资源的名字获取其ID数组
	 * 
	 * @author huangb
	 *
	 */
	public static int[] getIdArrByName(Context context, String className, String name) {
		String packageName = context.getPackageName();
		Class r = null;
		int[] id = { 0 };
		try {
			r = Class.forName(packageName + ".R");

			Class[] classes = r.getClasses();
			Class desireClass = null;

			for (int i = 0; i < classes.length; ++i) {
				if (classes[i].getName().split("\\$")[1].equals(className)) {
					desireClass = classes[i];
					break;
				}
			}

			if (desireClass != null)
				id = (int[]) desireClass.getField(name).get(desireClass);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		}

		return id;
	}

	public static String getString(int resId) {
		return getResources().getString(resId);
	}

	public static int getInteger(int resId) {
		return getResources().getInteger(resId);
	}

	public static float getDimension(int resId) {
		return getResources().getDimension(resId);
	}

	public static int getColor(int resId) {
		return getResources().getColor(resId);
	}

	@SuppressWarnings("deprecation")
	public static Drawable getDrawable(int resId) {
		return getResources().getDrawable(resId);
	}

	public static ColorStateList getColorStateList(int resId) {
		return getResources().getColorStateList(resId);
	}

	public static String[] getStringArray(int resId) {
		return getResources().getStringArray(resId);
	}

	public static Bitmap getBitmap(int resId) {
		return BitmapFactory.decodeResource(getResources(), resId);
	}
}
